﻿
/*
 * PsTFS is a set of PowerShell commands to manage and use Team Foundation Server. PowerShell 
 * effectiveness is combined with the power of TFS API to maximizes TFS capabilities.
 * 
 * Creator : Choulant Noham
 * Mail : choulant@gmail.com
 * Blog : http://choulant.blogspot.com
 * 
 */

using System;
using System.Collections.Generic;
using System.Management.Automation;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.Server;
using Microsoft.TeamFoundation.VersionControl.Client;
using PsTFS.Methodes;
using PsTFS.Provider;
using PsTFS.Objects;


namespace PsTFS.Commandes.Add
{
    /// <summary>
    /// Permet d'ajouter des membres au projet
    /// </summary>
    [Cmdlet(VerbsCommon.Add, "MemberProject")]
    public class Add_MemberProject : PsCmdlet
    {
        /// <summary>
        /// Constructeur
        /// </summary>
        public Add_MemberProject()
        {
            this.UserName = string.Empty;
            this.Roles = string.Empty;
        }
        /// <summary>
        /// Processus de création
        /// </summary>
        protected override void ProcessRecord()
        {
            try
            {
                //Ajoute l'utilisateur
                Methode.AddUserToRole(this.UserName, this.Roles, Context.Instance.Project.Name);

            }
            catch (Exception ex)
            {
                ErrorRecord err = new ErrorRecord(ex, Error.GetInstance().GetError(this), ErrorCategory.WriteError, null);
                WriteError(err);
                Log.AddLog(ex.StackTrace);
            }
            finally
            {
                base.ProcessRecord();
            }
        }

        /// <summary>
        /// Permet d'informer la fin de la commande
        /// </summary>
        protected override void EndProcessing()
        {
            Log.AddLog(null, "Add-MemberProject is end");
            base.EndProcessing();
        }

        /// <summary>
        /// Object contenant Team Foundation Server
        /// </summary>
        [Parameter(Position = 0)]
        public TfsTeamProjectCollection TFS
        {
            set { Context.Instance.Tfs = value; }
        }
        /// <summary>
        /// Nom du label
        /// </summary>
        [Parameter(Position = 1)]
        public string ProjectName
        {
            set { Context.Instance.Project = Methode.GetProject(value); }
        }

        /// <summary>
        /// Login de l'utilisateur a ajouter
        /// </summary>
        [Parameter(Position = 2, Mandatory = true)]
        [ValidateNotNullOrEmpty]
        public string UserName { get; set; }

        /// <summary>
        /// Groupe dans les quels ajouter l'utilisateur
        /// </summary>
        [Parameter(Position = 3, Mandatory = true)]
        [ValidateNotNullOrEmpty]
        public string Roles { get; set; }
    }
}


